<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <div class="login-form">
        <div class="login-header">
          <img src="@/assets/logo.png" alt="logo" class="logo" />
          <h1 class="title">星遇校园后台管理系统</h1>
          <p class="subtitle">专业的校园管理平台</p>
        </div>

        <a-form ref="formRef" :model="loginForm" :rules="rules" @finish="handleLogin">
          <a-form-item name="phone">
            <a-input
              v-model:value="loginForm.phone"
              size="large"
              placeholder="请输入手机号码"
              allow-clear
            >
              <template #prefix>
                <UserOutlined class="input-icon" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-button type="primary" html-type="submit" size="large" block :loading="loading">
              登录
            </a-button>
          </a-form-item>
        </a-form>

        <!-- 测试账号提示 -->
        <div class="test-info">
          <a-divider>测试账号</a-divider>
          <div class="test-accounts">
            <p><strong>超级管理员:</strong> 13800138001</p>
            <p><strong>学校管理员:</strong> 13800138002</p>
          </div>
        </div>
      </div>
    </div>

    <div class="login-bg">
      <div class="bg-content">
        <h2>星遇校园</h2>
        <p class="welcome-text">欢迎使用后台管理系统</p>
        <div class="feature-tags">
          <span class="tag">智能管理</span>
          <span class="tag">安全可靠</span>
          <span class="tag">简单易用</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { UserOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import type { Rule } from 'ant-design-vue/es/form'
import { useAuthStore } from '@/stores/auth'
import { authService } from '@/api/auth'
import type { LoginForm, LoginResponse } from '@/types'

const router = useRouter()
const authStore = useAuthStore()

// 响应式数据
const loading = ref(false)
const formRef = ref()

// 登录表单
const loginForm = reactive<LoginForm>({
  phone: '13800138001',
})

// 表单验证规则
const rules: Record<string, Rule[]> = {
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
  ],
}

// 登录处理
const handleLogin = async () => {
  try {
    loading.value = true

    const loginResponse = await authService.adminLogin(loginForm)

    const loginRes: LoginResponse = {
      token: loginResponse.token,
      id: loginResponse.id,
      avatar: loginResponse.avatar,
      nickName: loginResponse.nickName,
    }

    // 保存登录信息
    authStore.login(loginRes)

    message.success('登录成功！')
  } catch (error) {
    console.error('登录失败:', error)
    message.error('登录失败，请检查手机号码')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  background-image: url('@/assets/bg1.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-form-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
}

.login-form {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 48px 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;

  .logo {
    width: 72px;
    height: 72px;
    margin-bottom: 20px;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  }

  .title {
    font-size: 28px;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 8px;
    letter-spacing: -0.5px;
  }

  .subtitle {
    color: #666;
    font-size: 15px;
    margin: 0;
    font-weight: 400;
  }
}

.input-icon {
  color: #bfbfbf;
}

:deep(.ant-input-affix-wrapper) {
  border-radius: 8px;
  border: 1px solid #dee2e6;
  padding: 12px 16px;
  background: rgba(248, 249, 250, 0.8);

  &:hover {
    border-color: #6c757d;
    background: rgba(248, 249, 250, 0.9);
  }

  &:focus-within {
    border-color: #495057;
    box-shadow: 0 0 0 2px rgba(108, 117, 125, 0.15);
    background: rgba(255, 255, 255, 0.95);
  }
}

:deep(.ant-input) {
  background: transparent;
}

:deep(.ant-btn-primary) {
  background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
  border: none;
  border-radius: 8px;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(108, 117, 125, 0.25);

  &:hover {
    background: linear-gradient(135deg, #5a6268 0%, #3d4246 100%);
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(108, 117, 125, 0.35);
  }

  &:active {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(108, 117, 125, 0.3);
  }

  &:focus {
    box-shadow: 0 0 0 2px rgba(108, 117, 125, 0.2);
  }
}

.test-info {
  margin-top: 32px;

  :deep(.ant-divider) {
    margin: 16px 0;
    border-color: #dee2e6;

    .ant-divider-inner-text {
      font-size: 13px;
      color: #6c757d;
    }
  }

  .test-accounts {
    font-size: 13px;
    color: #495057;
    text-align: center;
    background: rgba(248, 249, 250, 0.6);
    border-radius: 8px;
    padding: 16px;
    border: 1px solid rgba(222, 226, 230, 0.5);

    p {
      margin: 6px 0;
    }

    strong {
      color: #343a40;
    }
  }
}

.login-bg {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  background: rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(5px);

  .bg-content {
    color: white;
    text-align: center;
    max-width: 400px;

    h2 {
      font-size: 56px;
      font-weight: 700;
      margin-bottom: 20px;
      color: white;
      text-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
      letter-spacing: -1px;
    }

    .welcome-text {
      font-size: 20px;
      margin-bottom: 40px;
      opacity: 0.9;
      font-weight: 300;
    }

    .feature-tags {
      display: flex;
      gap: 12px;
      justify-content: center;
      flex-wrap: wrap;

      .tag {
        background: rgba(255, 255, 255, 0.15);
        backdrop-filter: blur(10px);
        padding: 8px 16px;
        border-radius: 20px;
        font-size: 14px;
        font-weight: 400;
        border: 1px solid rgba(255, 255, 255, 0.2);
        transition: all 0.3s ease;

        &:hover {
          background: rgba(255, 255, 255, 0.25);
          transform: translateY(-2px);
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .login-bg {
    order: -1;
    flex: none;
    min-height: 240px;

    .bg-content {
      h2 {
        font-size: 40px;
      }

      .welcome-text {
        font-size: 16px;
      }

      .feature-tags {
        gap: 8px;

        .tag {
          padding: 6px 12px;
          font-size: 13px;
        }
      }
    }
  }

  .login-form-wrapper {
    flex: none;
    padding: 20px;
  }

  .login-form {
    max-width: 100%;
    padding: 32px 24px;
  }
}

@media (max-width: 480px) {
  .login-form {
    padding: 24px 20px;
  }

  .login-header {
    .title {
      font-size: 24px;
    }

    .logo {
      width: 60px;
      height: 60px;
    }
  }
}
</style>